home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Simple_Module2 / MenuWindow / MenuWindow.c < prev    next >
C/C++ Source or Header  |  1998-08-28  |  6KB  |  181 lines

  1. /*******************************************************************
  2.  
  3.    MenuWindow.c
  4.     
  5.     Does open a simple window with some easy gadgets and a very
  6.     simple menu.
  7.         
  8.             
  9. *********************************************************************/
  10.  
  11. #include "MenuWindow.h"
  12.  
  13. /********************************************************************/
  14. // locale prototypes
  15.  
  16. BOOL OpenDOpusWin( WindowHandle *wh );
  17.  
  18. BOOL HandleWindow( WindowHandle *wh );
  19.  
  20. BOOL HandleMenu( WindowHandle *wh ); 
  21.  
  22. /********************************************************************/
  23.  
  24. void OwnWindow( STRPTR args, struct Screen *screen )
  25. {
  26.     WindowHandle          *wh;
  27.     
  28.     if( (wh = AllocMemH(mempool, sizeof(WindowHandle))) ) // allocate some memory
  29.       {
  30.           wh->screen = screen;     // store the screen pointer
  31.           
  32.           if( OpenDOpusWin(wh) )   // open the window
  33.            {
  34.                  // we copy now the arguments into the text gadget
  35.                // if we have supplied some...
  36.                  
  37.                SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) args );
  38.                                                    
  39.                while( TRUE ) 
  40.                   {
  41.                         // I use here Wait(), so it is easier with the changes
  42.                  
  43.                       wh->signals = Wait( 1 << wh->win->UserPort->mp_SigBit );   // wait for window events
  44.                                                         
  45.                       if( wh->signals & 1 << wh->win->UserPort->mp_SigBit )
  46.                             if( HandleWindow(wh) )
  47.                                    break;        // does end the while loop                                       
  48.                      
  49.                   }
  50.              
  51.                CloseConfigWindow( wh->win ); 
  52.              }
  53.              
  54.           FreeMemH( wh ); // free our memory
  55.       }                            
  56. }
  57.  
  58. /********************************************************************/
  59. // This function does open our window. We could have done this in the
  60. // function OwnWindow() too, but we need this function later again.
  61.  
  62. BOOL OpenDOpusWin( WindowHandle *wh )
  63. {
  64.     NewConfigWindow ncfgwin;     // we need a NewConfigWindow structure too
  65.                                  // of couse you could also allocate it with
  66.                                           // AllocMemH()...
  67.     
  68.     // and have to fill it
  69.     
  70.     ncfgwin.nw_Parent = wh->screen;  // open on this screen
  71.     
  72.     // getting a localized title...
  73.     ncfgwin.nw_Title  = DOpusGetString( locale, MSG_WINDOW_TITLE );
  74.     
  75.     ncfgwin.nw_Dims   = &cfgwin; // a pointer to the ConfigWin structure
  76.     ncfgwin.nw_Locale = locale;  // the module locale pointer (from modinit.c)
  77.     ncfgwin.nw_Port   = NULL;    // we doesn't supply a port
  78.     ncfgwin.nw_Font   = NULL;    // just taking the screen font
  79.     ncfgwin.nw_Flags  = WINDOW_REQ_FILL      | // fill with stripple pattern
  80.                         WINDOW_AUTO_KEYS     | // handle keys automatic
  81.                               WINDOW_SCREEN_PARENT;  // nw_Parent points to a screen
  82.     
  83.     if( (wh->win = OpenConfigWindow(&ncfgwin)) )   // open the window
  84.       {
  85.           if( (wh->olist = AddObjectList(wh->win, odef)) ) // add the gadgets
  86.             {
  87.                  // now we add the menu...
  88.                  
  89.                  AddWindowMenus( wh->win, winmenu );
  90.                  
  91.                   return TRUE;
  92.              }
  93.                     
  94.           CloseConfigWindow( wh->win );    //    in error case do not forget :-)
  95.       }
  96.     
  97.     return FALSE;
  98. }
  99.  
  100.  
  101. /********************************************************************/
  102. // we does only close the window, if the closegadget was pressed
  103. // if you want to close it within a gadget, you must only in the
  104. // right case set "stop" to TRUE
  105.  
  106. BOOL HandleWindow( WindowHandle *wh )
  107. {
  108.      BOOL stop = FALSE;
  109.      ULONG value;
  110.      
  111.      while( !stop && (wh->imsg = GetWindowMsg(wh->win->UserPort)) )
  112.         {
  113.             switch( wh->imsg->Class )  // let's handle the IDCMP
  114.               {
  115.                   case IDCMP_GADGETUP:
  116.                                           switch( GET_ID(wh->imsg) )
  117.                                          {
  118.                                                          case GADGET_ID_CYCLE:  // we copy simply the same text to the text gadget
  119.             
  120.                                                                      value = GetGadgetValue( wh->olist, GADGET_ID_CYCLE ) + MSG_CLICK_ME;
  121.                                                                             SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, value) );
  122.                                                                             break;
  123.             
  124.                                               case GADGET_ID_OKAY:   // doing a message
  125.                                                                      
  126.                                                                                         SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_OKAY_DONE) );
  127.                                                                             break;
  128.                                                      
  129.                                               case GADGET_ID_CANCEL: 
  130.                                                                      SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_CANCEL_DONE) );
  131.                                                                             break;
  132.                                           }
  133.                                                                  
  134.                                                   break;
  135.                                      
  136.                   case IDCMP_CLOSEWINDOW:
  137.                                           // we can not simply return here, the IntuiMessage must replied first
  138.                                                   
  139.                                                stop = TRUE;
  140.                                                   break;        
  141.                                                   
  142.                   case IDCMP_MENUPICK:
  143.                                           stop = HandleMenu( wh );
  144.                                                   break;                             
  145.               }
  146.                                  
  147.             ReplyWindowMsg( wh->imsg );  
  148.                               
  149.             // remember: You should not use any other routines
  150.             // to get/reply the messages of this window than
  151.             // GetWindowMsg() and ReplyWindowMsg() !!
  152.        }
  153.                                
  154.     return stop;
  155. }
  156.  
  157. /********************************************************************/
  158.  
  159. BOOL HandleMenu( WindowHandle *wh )
  160. {
  161.      switch( GET_MENUID(wh->win, wh->imsg) )
  162.         {
  163.              case MENU_ID_SAVE: // doing a simple thing
  164.                                 
  165.                                       DisplayBeep(NULL);              
  166.                                 break;
  167.                                       
  168.              case MENU_ID_QUIT: // we does really quit here
  169.              
  170.                                 return TRUE;
  171.                                                           
  172.              case MENU_ID_ITEM: // I leave it here empty
  173.                                 // it should be no problem for you to
  174.                                       // insert something... 
  175.                                 
  176.                                 break;
  177.         }
  178.          
  179.       return FALSE;
  180. }
  181.